using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.ComponentModel;
using System.Data;

namespace TanashCMS.Business
{
    public class WebPage : CMSDataObject
    {
        private string mPageName;
        private int mParentPageID;
        private bool mIsSecure;
        [NonSerialized(), System.Xml.Serialization.SoapIgnore(), System.Xml.Serialization.XmlIgnore()]
        private TanashWebsite mWebsite = null;
        private WebPageType mPageType = WebPageType.Standard;
        private PageTemplateType mTemplateType = PageTemplateType.MasterPage;
        private bool mIsBlogPost;
        private DateTime mDateCreated = DateTime.Now;

        private string mSmartPageLayoutControl = "";
        private int mPageLayoutColumns = 2;

        [System.Xml.Serialization.SoapIgnore(), System.Xml.Serialization.XmlIgnore()]
        public virtual TanashWebsite Website { get { return mWebsite; } set { mWebsite = value; } }
        public virtual WebPageType PageType { get { return mPageType; } set { mPageType = value; } }
        public virtual string SmartPageLayoutControl { get { return mSmartPageLayoutControl; } set { mSmartPageLayoutControl = value; } }
        public virtual int LayoutColumns { get { return mPageLayoutColumns; } set { mPageLayoutColumns = value; } }

        public WebPage()
        {
            
        }

        public virtual string PageName
        {
            get { return mPageName; }
            set { mPageName = value; }
        }

        public virtual int ParentPageID
        {
            get { return mParentPageID; }
            set { mParentPageID = value; }
        }

        public virtual bool IsBlogPost
        {
            get { return mIsBlogPost; }
            set { mIsBlogPost = value; }
        }

        public virtual bool IsSecure
        {
            get
            {
                return mIsSecure;
            }
            set
            {
                mIsSecure = value;
            }
        }
        public virtual DateTime DateCreated { get { return mDateCreated; } set { mDateCreated = value; } }

        public PageTemplateType TemplateType { get { return mTemplateType; } set { mTemplateType = value; } }

        public static List<WebPage> List(int WebsiteID, bool Recursive)
        {
            string query = "select wp from WebPage wp";
            query += " where wp.Website.ID = " + WebsiteID.ToString();
            if (!Recursive)
                query += " and wp.ParentPageID = 0";
            query += " and wp.IsBlogPost = 0";
            query += " order by wp.Title";
            return (List<WebPage>)Configuration.GetCurrentSession().CreateQuery(query).List<WebPage>();
        }
        public static List<WebPage> List(int WebsiteID, int ParentPageID)
        {
            return (List<WebPage>)Configuration.GetCurrentSession().CreateQuery("select wp from WebPage wp where wp.Website.ID = " + WebsiteID.ToString() + " and wp.ParentPageID = " + ParentPageID.ToString() + " and wp.IsBlogPost = 0 order by wp.Title").List<WebPage>();
        }

        public static List<WebPage> GetAllBlogPosts(int WebsiteID)
        {
            return (List<WebPage>)Configuration.GetCurrentSession().CreateQuery("select wp from WebPage wp where wp.Website.ID = " + WebsiteID.ToString() + " and wp.IsBlogPost = 1 order by wp.DateCreated desc").List<WebPage>();
        }
        public static List<WebPage> GetPublishedBlogPosts(int WebsiteID)
        {
            return (List<WebPage>)Configuration.GetCurrentSession().CreateQuery("select wp from WebPage wp where wp.Website.ID = " + WebsiteID.ToString() + " and wp.IsBlogPost = 1 and wp.State = " + ((int)CMSObjectState.Published).ToString() + " order by wp.DateCreated desc").List<WebPage>();
        }


        public static WebPage Select(int WebPageID)
        {
            return Configuration.GetCurrentSession().Get<WebPage>(WebPageID);
        }

        public static void Update(WebPage WebPage)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                if (WebPage.Website == null)
                {
                    WebPage originalWebPage = session.Get<WebPage>(WebPage.ID);
                    WebPage.Website = originalWebPage.Website;
                    session.Evict(originalWebPage);
                }
                session.SaveOrUpdate(WebPage);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }
        public static void Insert(WebPage WebPage)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                WebPage.GUID = Guid.NewGuid().ToString();
                session.Save(WebPage);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }
        public static void Delete(WebPage WebPage)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Delete(WebPage);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }


    }
}
